<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="depts"
        :props="props"
        :expand-on-click-node="false"
        icon-class="el-icon-office-building"
        default-expand-all
        highlight-current
      >
        <template #default="{data}">
          <el-row style="width: 100%" type="flex" justify="between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col align="center">{{ data.type }}</el-col>
            <el-col align="center">{{ data.managerName }}</el-col>
            <el-col align="center">{{ data.member || 0 }}人</el-col>
            <el-col align="center">
              <el-button type="text">编辑</el-button>
              <el-button type="text" style="margin: 0 10px" @click="del(data.id)">删除</el-button>
              <el-button type="text" @click="addDepartment('add',data.id)">新增子项</el-button>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept :show-dialog.sync="showDialog" @update="getDepartments" />
  </div>
</template>

<script>
import { getDepartmentList, delDepartment } from '@/api/homework/zhuzhijiagou'
import AddDept from '@/views/homework/components/add-dept.vue'

export default {

  components: {
    AddDept
  },
  data() {
    return {
      // 显示与隐藏
      showDialog: false,
      // 组织架构列表数据
      depts: [],
      // 读取数据的字段名
      props: { label: 'name' }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
  // 获取组织架构列表数据
    async getDepartments() {
      // 调用组织架构列表接口
      const res = await getDepartmentList()
      // console.log(res)
      this.depts = res.data
    },
    // 添加数据
    addDepartment() {
      this.showDialog = true
    },
    // 删除数据
    del(id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delDepartment(id)
        // 3. 提示用户
        this.$message.success('删除数据成功')
        // 4. 刷新列表
        this.getDepartments()
      })
    }
  }
}
</script>

<style>

</style>
